package com.lsh.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsh.common.util.PageSurport;
import com.lsh.dao.TsysMenuDao;
import com.lsh.model.TsysMenu;
import com.lsh.model.TsysRight;

@Service("tsysMenuService")
public class TsysMenuService extends BasicService{

	private final static Log LOG = LogFactory.getLog(TsysMenu.class);
	@Autowired
	TsysMenuDao dao;
	
	
	/**
	 * 插入数据
	 * @param tsysMenu
	 * @return
	 */
	public int insert(TsysMenu tsysMenu) {
		return dao.insert("TsysMenu-insert", tsysMenu);
	}

	/**
	 * 根据ID获取记录
	 * @param tsysMenu
	 * @return
	 */
	public TsysMenu getById(String no) {
		return dao.getById("TsysMenu-getById", no);
	}

	/**
	 * 删除记录
	 * @param object
	 * @return
	 */
	public int delete(TsysMenu tsysMenu) {
		return dao.delete("TsysMenu-delete", tsysMenu);
	}

	/**	更新记录
	 * @param tsysUser
	 */
	public void update(TsysMenu tsysMenu) {
		dao.update("TsysMenu-update", tsysMenu);
	}
	
	/**
	 * 获取分页数据
	 * @param map
	 * @param page
	 * @return
	 */
	public PageSurport getPage(Map map, PageSurport page) {
		return dao.selectPageByPage("getPage",
				"TsysMenu-getPageCount", page, map);
	}

	/**获取全部记录
	 * @param orderBy
	 * @return
	 */
	public List getAll(String orderBy) {
		return dao.selectAll("TsysMenu-getAll", orderBy);
	}

	private void digui(TsysMenu menu) {
		String parentid = menu.getMenuid();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentid", parentid);
		List<TsysMenu> list = this.getAllBy(map);
		if (list==null || list.size() <= 0) {
			return;
		} else {
			menu.setChildMenus(list);
			for (TsysMenu menu2 : list) {
				digui(menu2);
			}
		}
	}

	private List<TsysMenu> getAllBy(Map<String, Object> map) {
		return dao.selectList("TsysMenu-getAllBy", map);
	}

	public TsysMenu getTree() {
		TsysMenu menu = this.getById("Root");
		digui(menu);
		System.out.println(menu.getChildMenus().size());
		TsysMenu menu2 = (TsysMenu) menu.getChildMenus().get(0);
//		System.out.println(menu2.getChildMenus().get(0).getChildMenus().get(0).getMenuname());
		return menu;
	}
	
}
